import {
  currentTenantApiPermGroup, listApi,
  listRoleType, listRole, removeRoles,
  getPermGroup, getRoleGroup, createRole,
  listResGroup, removeRes, listCreateTenantOfRole,
  listApiRole, listCreateTenantOfApiPerm, listApiPerm,
  removeApiPerm
} from '@/api/userAuth'

const getDefaultState = () => { }

const state = getDefaultState()

const mutations = {}

const actions = {
  // 获取当前租户权限资源组
  getCurrentTenantApiPermGroup() {
    return new Promise((resolve, reject) => {
      currentTenantApiPermGroup().then(res => {
        resolve(res);
      }).catch(err => {
        reject(err);
      })
    })
  },
  // 获取api接口
  getApiList() {
    return new Promise((resolve, reject) => {
      listApi().then(res => {
        resolve(res);
      }).catch(err => {
        reject(err);
      })
    })
  },
  // 获取角色类型
  getRoleTypeList() {
    return new Promise((resolve, reject) => {
      listRoleType().then(res => {
        resolve(res);
      }).catch(err => {
        reject(err);
      })
    })
  },
  // 获取角色列表
  getRoleList({ commit }, searchItem) {
    return new Promise((resolve, reject) => {
      listRole(searchItem).then(res => {
        resolve(res);
      }).catch(err => {
        reject(err);
      })
    })
  },
  // 删除角色
  removeRoles({ commit }, roleIds) {
    return new Promise((resolve, reject) => {
      removeRoles(roleIds).then(res => {
        resolve(res);
      }).catch(err => {
        reject(err);
      })
    })
  },
  // 获取权限组
  getPermGroup({ commit }, data) {
    return new Promise((resolve, reject) => {
      getPermGroup(data).then(res => {
        resolve(res);
      }).catch(err => {
        reject(err);
      })
    })
  },
  // 获取角色组
  getRoleGroup({ commit }, data) {
    return new Promise((resolve, reject) => {
      getRoleGroup(data).then(res => {
        resolve(res);
      }).catch(err => {
        reject(err);
      })
    })
  },
  // 创建角色
  createRole({ commit }, data) {
    return new Promise((resolve, reject) => {
      createRole(data).then(res => {
        resolve(res);
      }).catch(err => {
        reject(err);
      })
    })
  },
  // 获取资源组列表
  listResGroup({ commit }, roleId) {
    return new Promise((resolve, reject) => {
      listResGroup(roleId).then(res => {
        resolve(res);
      }).catch(err => {
        reject(err);
      })
    })
  },
  // 删除权限组资源
  removeRes({ commit }, resData) {
    return new Promise((resolve, reject) => {
      removeRes(resData).then(res => {
        resolve(res);
      }).catch(err => {
        reject(err);
      })
    })
  },
  // 获取角色创建人
  listCreateTenantOfRole() {
    return new Promise((resolve, reject) => {
      listCreateTenantOfRole().then(res => {
        resolve(res);
      }).catch(err => {
        reject(err);
      })
    })
  },
  // 获取api角色列表
  listApiRole() {
    return new Promise((resolve, reject) => {
      listApiRole().then(res => {
        resolve(res);
      }).catch(err => {
        reject(err);
      })
    })
  },
  // 获取api权限创建人列表
  listCreateTenantOfApiPerm() {
    return new Promise((resolve, reject) => {
      listCreateTenantOfApiPerm().then(res => {
        resolve(res);
      }).catch(err => {
        reject(err);
      })
    })
  },
  // 获取api权限列表
  listApiPerm({ commit }, searchPermData) {
    return new Promise((resolve, reject) => {
      listApiPerm(searchPermData).then(res => {
        resolve(res);
      }).catch(err => {
        reject(err);
      })
    })
  },
  // 通过id删除api权限
  removeApiPerm({ commit }, removePermData) {
    return new Promise((resolve, reject) => {
      removeApiPerm(removePermData).then(res => {
        resolve(res);
      }).catch(err => {
        reject(err);
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

